package com.ky.pro.java.nio.http.hello.controller;

import org.springframework.web.bind.annotation.*;

/**
 * @ClassName HelloController
 * @Description TODO
 * @Date 2021/11/20 19:43
 * @Author kevin
 **/
@RestController
@RequestMapping("/hello")
public class HelloController {

    @GetMapping("/{word}")
    public String greetWithWord(@RequestHeader(value = "greetings", required = false) String headerGreeting,
                                @PathVariable String word) {
        if(headerGreeting != null) {
            word = word + ", I heard your greeting: " + headerGreeting;
        }
        return genResponse(word);
    }

    @GetMapping("/question")
    public String askQuestion(@RequestParam String content) {
        return genResponse(content + "?");
    }

    @PostMapping("")
    public String greetWithWordInBody(@RequestBody String word) {
        return genResponse(word);
    }

    private String genResponse(String word) {
        return "{\"status\":\"ok\", \"data\": {\"greeting\":\"hello, " + word + "!\"}}";
    }

}
